package com.zucc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zucc.controller.req.CategoryAddReq;
import com.zucc.controller.req.CategoryModifyReq;
import com.zucc.controller.req.CategorySearchReq;
import com.zucc.controller.resp.CategoryResp;
import com.zucc.entity.Category;
import com.zucc.entity.CategoryMapper;
import com.zucc.entity.base.MyMapper;
import com.zucc.exception.SyzxException;
import com.zucc.service.CategoryService;
import com.zucc.service.base.MyServiceImpl;
import com.zucc.service.base.PageData;
import com.zucc.util.MyPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-12-11 8:56 下午
 */
@Service
public class CategoryServiceImpl extends MyServiceImpl<Category> implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public MyMapper<Category> mapper() {
        return categoryMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CategoryAddReq req) {
        Category category = new Category();
        BeanUtil.copyProperties(req, category);
        category.setDelFlag(0);
        category.setCreateTime(DateUtil.date());
        category.setModifyTime(DateUtil.date());
        super.saveKey(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(CategoryModifyReq req) {
        Category categoryExist = super.findById(req.getId());
        if (Objects.isNull(categoryExist)) {
            throw new SyzxException("系统找不到相应编号的分类信息");
        }
        BeanUtil.copyProperties(req, categoryExist, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        categoryExist.setModifyTime(DateUtil.date());
        super.updateSelective(categoryExist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOne(Long id) {
        Category categoryExist = super.findById(id);
        if (Objects.isNull(categoryExist)) {
            throw new SyzxException("系统找不到相应编号的分类信息");
        }
        categoryExist.setDelFlag(1);
        categoryExist.setDeleteTime(DateUtil.date());
        super.updateSelective(categoryExist);
    }

    @Override
    public PageData<CategoryResp> page(CategorySearchReq req) {
        Example example = new Example(Category.class);
        example.setOrderByClause("modify_time DESC");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", 0);

        if (StrUtil.isNotBlank(req.getName())) {
            criteria.andLike("name", "%" + req.getName() + "%");
        }
        if (StrUtil.isNotBlank(req.getDescription())) {
            criteria.andLike("description", "%" + req.getDescription() + "%");
        }
        if (StrUtil.isNotBlank(req.getRemarks())) {
            criteria.andLike("remarks", "%" + req.getRemarks() + "%");
        }

        PageData<Category> pageData = super.findByPage(req, example);
        PageData<CategoryResp> convert = MyPageUtil.convert(pageData, CategoryResp.class);
        return convert;
    }
}
